package com.ayuan.blog.service.impl;

import com.ayuan.blog.bean.TagInfo;
import com.ayuan.blog.dao.ArticleTagDao;
import com.ayuan.blog.dao.TagDao;
import com.ayuan.blog.domain.ArticleTag;
import com.ayuan.blog.domain.Tag;
import com.ayuan.blog.service.ArticleTagService;
import org.apache.commons.collections.CollectionUtils;
import org.durcframework.core.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleTagServiceImpl 
        extends CrudService<ArticleTag, ArticleTagDao> 
        implements ArticleTagService {

    @Autowired
    private TagDao tagDao;

    @Override
    public List<ArticleTag> findListByArticleIds(List<Integer> articleIdList) {
        if (CollectionUtils.isEmpty(articleIdList)) {
            return new ArrayList<ArticleTag>();
        }
        return this.getDao().findListByArticleIds(articleIdList);
    }

    @Override
    public List<TagInfo> findTagInfoList(Integer limit) {
        if (null == limit || limit <= 0) {
            limit = 8;
        }
        List<TagInfo> tagInfoList = this.getDao().findTagInfoList(limit);

        List<Integer> tagIdList = new ArrayList<Integer>();

        if (CollectionUtils.isNotEmpty(tagInfoList)) {
            for (TagInfo tagInfo : tagInfoList) {
                tagIdList.add(tagInfo.getTagId());
            }
            List<Tag> tagList = tagDao.findListByIds(tagIdList);
            for (TagInfo tagInfo : tagInfoList) {
                for (Tag tag : tagList) {
                    if (tagInfo.getTagId() == tag.getId()) {
                        tagInfo.setTagName(tag.getTagName());
                        break;
                    }
                }
            }
        }

        return tagInfoList;
    }
}